package com.codedef.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.codedef.entity.Seat;
import com.codedef.entity.SeatVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

@Mapper
public interface SeatMapper extends BaseMapper<Seat> {

    @Select("SELECT reservation_record.seat_id FROM reservation_record, seat " +
            "WHERE reservation_record.seat_id = seat.id " +
            "AND (NOW() BETWEEN reservation_record.start_time AND reservation_record.end_time) " +
            "AND seat.classroom_id = #{classroomId}")
    List<Integer> selectNowReservationSeatIdListByClassroomId(@Param("classroomId") Integer classroomId);

    @Select("SELECT abbreviation FROM classroom WHERE id = (SELECT classroom_id FROM seat WHERE id = #{seatId}) ")
    String selectClassroomAbbreviationBySeatId(@Param("seatId") Integer seatId);


    @Select("SELECT seat.id, seat.abbreviation, seat.sequence, seat.state, floor.`name` AS floor_name, classroom.`name` AS classroom_name FROM floor, classroom, seat " +
            "WHERE floor.id = classroom.floor_id AND classroom.id = seat.classroom_id " +
            "AND seat.id NOT IN ( " +
            "SELECT seat_id FROM reservation_record WHERE end_time BETWEEN #{timeA} AND #{timeB} " +
            "OR start_time BETWEEN #{timeA} AND #{timeB} " +
            "OR start_time < #{timeA} AND end_time > #{timeB} ) ")
    List<SeatVo> selectFreeByTimePeriod(@Param("timeA") Date timeA, @Param("timeB") Date timeB);

}
